<template>
  <div>
    <BasicTable
      @register="registerTable"
      title="组织信息管理"
      :pagination="false"
      :scroll="{ x: false, y: 'calc(100vh - 300px)' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="onAddOrganizationButtonClick">新增</a-button>
      </template>
      <template #deptid="{ column, record }">
        {{
          deptData.filter((item) => item.value == record.deptid).length > 0
            ? deptData.filter((item) => item.value == record.deptid)[0].label
            : record.deptid
        }}
      </template>
      <template #orgtype="{ column, record }">
        {{
          orgType.filter((item) => item.value == record.orgtype).length > 0
            ? orgType.filter((item) => item.value == record.orgtype)[0].label
            : record.orgtype
        }}
      </template>
      <template #runtpl="{ column, record }">
        {{ returnDangerDbLabel(record.runtpl) }}
      </template>
      <template #contpl="{ column, record }">
        {{ returnDangerDbLabel(record.contpl) }}
      </template>
      <template #action="{ column, record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditOrganizationButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: onDeleteOrganizationButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddOrUpdateOrganization
      @register="registerAddOrUpdateOrganizationModal"
      @success="afterSuccess"
    />
  </div>
</template>

<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  import { useGlobSetting } from '/@/hooks/setting';
  import { Tag } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { queryOrganization, deleteOrganization } from '/@/api/ehs/organization';
  import { TplOrganizationTreeTableItemDto } from '/@/api/ehs/model/organizationModel';
  import { organizationTableColumns } from './organization.data';
  import { getProjectDeptList } from '/@/api/public/public';
  import { queryEnumOptions } from '/@/api/platform/enum';
  import AddOrUpdateOrganization from './AddOrUpdateOrganization.vue';
  import { queryDangerDb } from '/@/api/ehs/templateHazard';
  export default defineComponent({
    name: 'organizationIndex',
    components: { BasicTable, TableAction, Tag, AddOrUpdateOrganization },
    setup() {
      const deptData = ref([]);
      const { globalProjectCode } = useGlobSetting();
      const projectCode = ref<String>(globalProjectCode);
      const orgType = ref([]);
      const dangerDbOption = ref([]);
      const [registerAddOrUpdateOrganizationModal, { openModal: openAddOrUpdateOrganization }] =
        useModal();
      const [registerTable, { reload }] = useTable({
        api: queryOrganization,
        columns: organizationTableColumns,
        canResize: true,
        striped: true,
        showTableSetting: true,
        showIndexColumn: true,
        isTreeTable: false,
        bordered: true,
        ellipsis: true,
        indexColumnProps: {
          width: 50,
        },
        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });
      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }
      /**
       * 打开新增窗口
       */

      function onAddOrganizationButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '组织管理',
          operateType: OperationTypeEnum.ADD,
        };
        openAddOrUpdateOrganization(true, propsData);
      }
      /**
       * 打开编辑窗口
       * @param record
       */
      function onEditOrganizationButtonClick(record: TplOrganizationTreeTableItemDto) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '组织管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.orgcode,
        };
        openAddOrUpdateOrganization(true, propsData);
      }

      /**
       * 删除
       * @param record
       */
      function onDeleteOrganizationButtonClick(record: TplOrganizationTreeTableItemDto) {
        deleteOrganization(record.orgcode)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }
      function returnDangerDbLabel(code) {
        return dangerDbOption.value.filter((item) => item.hdbcd == code).length > 0
          ? dangerDbOption.value.filter((item) => item.hdbcd == code)[0].hdbnm
          : code;
      }
      onMounted(() => {
        getProjectDeptList(projectCode.value)
          .then((res) => {
            if (res) {
              deptData.value = res.map((item) => {
                return {
                  label: item.name,
                  value: item.id,
                };
              });
            }
          })
          .catch((e) => {});
        queryEnumOptions('EhsOrganizationTypeEnum').then((res) => {
          // console.log('option res', res);
          if (res != undefined && res != null && res.length > 0x0) {
            orgType.value = res[0].options.map((option) => {
              return {
                value: Number(option.key),
                label: option.value,
              };
            });
          }
        });
        queryDangerDb({ keytype: 0 }).then((res) => {
          dangerDbOption.value = res;
        });
      });
      return {
        orgType,
        deptData,
        dangerDbOption,
        returnDangerDbLabel,
        registerAddOrUpdateOrganizationModal,
        registerTable,
        afterSuccess,
        onAddOrganizationButtonClick,
        onEditOrganizationButtonClick,
        onDeleteOrganizationButtonClick,
      };
    },
  });
</script>

<style scoped></style>
